//
//  WelcomeViewController.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/17.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    
    //偷梁换柱
    override func loadView() {
        view = backImageView
    }
    
    //跟视图没有设置frame
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    
    //一般在viewDidAppear执行动画
    override func viewDidAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        print(view)
        //执行动画
        showAnimation()
    }
    
    private func setupUI(){
        
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        //设置约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(16)
        }
        
        //设置iconImage对应的图片 placeholder 占位符
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        
    }
    
    //执行动画，修改iconView的底部约束
    private func showAnimation(){
        
        /*
        自动布局的页面实现动画效果需要注意
        1.不能够直接修改frame,可能会引起自动布局系统计算错误
        2.直接修改视图的约束关系.自动布局系统会根据约束关系 自动计算空间的frame  在layoutSubViews方法中计算视图的frame
        3.每一次运行循环开启时  自动布局系统都会  '收集' 所有页面视图的约束的修改 不会立即更新约束
        在运行循环结束前  会自动调用 layoutsubViews方法 修改所有子视图的frame
        
        4.如果希望提前更新约束 需要强制刷新页面 self.view.layoutIfNeeded() 需要在动画的闭包中执行 并且 一定要在修改约束之后调用
        */
        
        welcomeLabel.alpha = 0
        let offset = -screenH + 200
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //执行约束的修改
            //自动布局 执行动画
            //强制刷新页面
            self.iconImage.snp_updateConstraints(closure: { (make) -> Void in
                
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            })

            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
               UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
                    print("OK")
                    //动画结束发出页面切换的通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
               })
        }
        
    }
    
    //懒加载所有子视图
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named:"ad_background"))
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    private lazy var welcomeLabel:UILabel = UILabel(title: (UserAccountViewModel().userName ?? "") + " 欢迎回来", color: UIColor.lightGrayColor(), fontSize: 16)
        

    
    
}
